package com.dingreading.cloud.subject.service.impl;

import com.dingreading.cloud.subject.entity.Stores;
import com.dingreading.cloud.subject.entity.table.StoresTableDef;
import com.dingreading.cloud.subject.mapper.StoresMapper;
import com.dingreading.cloud.subject.service.StoresService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-08-01
 */
@Service
public class StoresServiceImpl extends BaseServiceImpl<StoresMapper, Stores, StoresTableDef> implements StoresService {

    @Override
    protected StoresTableDef getTable() {
        return StoresTableDef.stores;
    }

    @Override
    public Stores getByCorpUidAndStoreUid(String corpUid, String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(corpUid))
            condition.and(table.corpUid.eq(corpUid));
        condition.and(table.uid.eq(storeUid))
                .and(table.enabled.eq(1));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public List<Long> getSubjectIdList(String storeUid) {
        QueryCondition condition = table.uid.eq(storeUid);
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.subjectType)
                .from(table)
                .where(condition);

        String subjectType = getMapper().selectObjectByQueryAs(wrapper, String.class);

        List<Long> subjectIdList = new ArrayList<>();
        if (StringUtils.isNotBlank(subjectType)) {
            subjectIdList = Arrays.stream(StringUtils.split(subjectType, ","))
                    .map(Long::valueOf).collect(Collectors.toList());
        }
        return subjectIdList;
    }

    @Override
    public List<Long> getCourseIdList(String storeUid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.courseType)
                .from(table)
                .where(table.uid.eq(storeUid));
        String courseType = getMapper().selectObjectByQueryAs(wrapper, String.class);
        List<Long> courseIdList = new ArrayList<>();
        if (StringUtils.isNotBlank(courseType)) {
            courseIdList = Arrays.stream(StringUtils.split(courseType, ","))
                    .map(Long::valueOf).collect(Collectors.toList());
        }
        return courseIdList;
    }
}
